<template>
	<view class="app-container">
		<view class="official-post app-box" v-for="item in postArr" :key="item.id" @click="toPostPage(item.id)">
			<view class="official-post-info">
				<view class="title">{{ item.title }}</view>
				<view class="time">{{ item.time }}</view>
			</view>
			<image v-if="item.images.length > 0" :src="item.images[0].url" mode="aspectFill" class="official-post-image"></image>
		</view>
	</view>
</template>

<script>
import request from '@/api/http.js';
import { timeCalc } from '@/api/utils.js';

export default {
	data() {
		return {
			postArr: []
		};
	},
	onShow() {
		this.getAllOfficialPost();
	},
	methods: {
		async getAllOfficialPost() {
			// 获取官方公告详情
			let res = await request(`/forum/getAnnouncements`);
			let postArr = res.data;
			postArr.forEach((item) => {
				item.time = timeCalc(item.createTime);
			});
			this.postArr = postArr;
		},
		toPostPage(id) {
			uni.navigateTo({
				url: `/pages/Forum/PostPage?id=${id}`
			});
		}
	}
};
</script>

<style lang="scss" scoped>
.official-post {
	display: flex;
	height: 120rpx;
	border-radius: 8px;

	&-info {
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		flex: 1;
		height: 100%;
		margin-right: 20rpx;

		.title {
			font-weight: bold;
			color: #333;
			display: -webkit-box;
			-webkit-box-orient: vertical;
			-webkit-line-clamp: 2;
			overflow: hidden;
			flex: 1;
		}

		.time {
			color: #999;
			margin-top: 10rpx;
			font-size: $font-size-small;
		}
	}

	&-image {
		width: 250rpx;
		height: 100%;
		border-radius: 8px;
	}
}
</style>
